<?php
include 'config.php';
include 'store_default.php';


$arr=array();
$arr_dress = array();

//查询商品表
$sql = "SELECT * FROM t_goods ORDER BY type_3, is_sell DESC, level ASC, is_new ASC, order_index ASC, goods_id ASC";
$rs = $db->getAll($sql);
foreach ($rs as $k=>$v)
{
	$type_1 = $v['type_1'];
	$type_2 = $v['type_2'];
	$type_3 = $v['type_3'];
	$arr[$type_1][$type_2][$type_3][] = $v;
}

//查询套装表
$sql = "SELECT * FROM t_goods_dress ORDER BY type_3, is_sell DESC, level ASC, order_index ASC, is_new ASC, id ASC";
$dress =$db->getAll($sql);
foreach ($dress as $k=>$v)
{	
	$strDress = $v['dress'];
	$strDress = str_replace("'", '"', $strDress);
	$v['dress'] = json_decode($strDress, true);
	
	$type_3 = $v['type_3'];
	$arr_dress[$type_3][]=$v;
	//sort($arr_dress[$v['type_3']]);
}

//把套装信息存商品数组
$arr['clothing']['dress']=$arr_dress;


CreateFolder($path_goods_info_txt);						//文件夹不存在则建立文件夹
$path_zip = $path_goods_info_txt . 'basic_txt.zip';  	//压缩文件路径
DeleteFile($path_zip);									//压缩文件存在, 先删除文件
	
//按type_1生成文本文件, 并压缩
foreach ($arr as $type_1=>$v)
{
	$txtFileName = $type_1 . '.txt';
	$txtFilePath = $path_goods_info_txt . $txtFileName;  		//文件名字为  type_1.txt
	DeleteFile($txtFilePath);									//文件存在, 先删除文件
	
	$strContent = json_encode($v);
	file_put_contents($txtFilePath, $strContent);  //把内容写文本
	
	zipFile($txtFileName, $path_zip, $strContent);		//把所有文本文件压缩成一个zip文件
	
	echo "成功生成: ". $txtFilePath . ", 压缩到zip文档: " . $path_zip. "</br>";
}

//生成default.txt, 并压缩
$txtFileName = 'default.txt';
$txtFilePath = $path_goods_info_txt . $txtFileName; 
DeleteFile($txtFilePath);//文件存在, 先删除文件
	
$strContent = json_encode($arrayDefaultGoods); //默认商品数组, 在store_default.php文件定义
file_put_contents($txtFilePath, $strContent);  //把内容写文本
zipFile($txtFileName, $path_zip, $strContent);		//把所有文本文件压缩成一个zip文件
echo "成功生成: ". $txtFilePath . ", 压缩到zip文档: " . $path_zip. "</br>";


//----通知服务端程序重新加载商场数据
$str = 'reload_data {}';
socketData ( $server, $port, $str );
echo "</br>已通知服务端程序重新加载商场数据...";


//----压缩文件
function zipFile($txtFileName, $zipFileName, $jsonStr){
	$zip = new ZipArchive ();
	if ($zip->open ($zipFileName, ZIPARCHIVE::CREATE ) !== TRUE) {
		die("cannot open <$zipFileName>\n" );
	}
	$zip->addFromString($txtFileName, $jsonStr);	
	$zip->close ();
}

?>
